<template>
  <div>
    <div class="header-box">
      <el-select v-model="year" size="mini" class="select-year" @change="dateChange">
        <el-option
          v-for="item in yearList"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
      <el-select v-model="month" size="mini" class="select-month" @change="dateChange">
        <el-option v-for="item in 12" :key="item" :label="item" :value="item" />
      </el-select>
    </div>
    <el-calendar v-model="currentDate">
      <!-- 这里使用的是 2.5 slot 语法，对于新项目请使用 2.6 slot 语法-->
      <!-- date是日期对象 -->
      <template #dateCell="{date}">
        <div>{{ date.getDate() }}</div>
        <span v-if="date.getDay() === 6 || date.getDay() === 0" class="reset">休</span>
      </template>
    </el-calendar>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 双向绑定当前年
      year: new Date().getFullYear(),
      yearList: [],
      //  双向绑定当前年
      month: new Date().getMonth() + 1,
      currentDate: new Date()
    }
  },
  created() {
    for (let i = this.year - 5; i <= this.year + 5; i++) {
      this.yearList.push(i)
    }
  },
  methods: {

    dateChange() {
      // 拿到最新年和最新月组成新的日期
      this.currentDate = new Date(`${this.year}-${this.month}-01`)
    }
  }
}
</script>

<style scoped lang="scss">
.header-box {
  text-align: right;

  .select-year {
    width: 110px;
  }
  .select-month {
    width: 80px;
    margin: 0 10px;
  }
}

.is-selected {
  color: #1989fa;
}

::v-deep {
  .el-calendar__header {
    display: none;
  }

  .el-calendar__body {
    th {
      font-weight: bold;

      &:nth-child(n + 6) {
        color: #fa7c4d;
      }
    }
    td {
      border: none !important;
    }
    .el-calendar-day {
      display: flex;
      justify-content: center;
      align-items: center;
    }

    .is-today {
      .el-calendar-day {
        div {
          border-radius: 50%;
          padding: 5px;
          background-color: #409eff;
          color: #fff;
        }
      }
    }
  }
}

.reset {
  border-radius: 50%;
  padding: 3px;
  background-color: #fa7c4d;
  color: #fff;
  font-size: 12px;
  margin-left: 5px;
}
</style>
